package com.onesports.intelligent.k12.polarlight.controller.wx.index;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.DisbandClassDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.SpellingClassDetailsDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.market.ProductCourseDetailsDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.CampusIdDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.CampusSearchDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.student.RemoveStudentDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.student.StuSpellClassDTO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CourseDateNumVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.MineSpellClassVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.MySpellClassVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.NearSpellClassVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.organization.CampusSearchVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.product.ProductCourseDetailsVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.product.ProductCourseVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.BannerConfigVO;
import com.onesports.intelligent.k12.polarlight.service.campus.OrganizationCampusService;
import com.onesports.intelligent.k12.polarlight.service.course.CourseScheduleService;
import com.onesports.intelligent.k12.polarlight.service.grade.ClassInfoService;
import com.onesports.intelligent.k12.polarlight.service.sys.BannerService;
import com.onesports.intelligent.k12.polarlight.service.sys.ProductService;
import com.onesports.intelligent.k12.polarlight.service.wx.SpellClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 校区搜索控制器
 *
 * @author clw
 * @date 2022-07-08 14:33:35
 */
@RequiredArgsConstructor
@RestController
@RequestMapping(ApiPathConstants.INDEX)
@Api(tags = "会员端-首页API")
@PreAuthorize("hasAuthority('MINI_APP')")
public class IndexController {

    private final SpellClassService spellClassService;
    private final OrganizationCampusService organizationCampusService;
    private final ProductService productService;
    private final ClassInfoService classInfoService;
    private final BannerService bannerService;
    private final CourseScheduleService courseScheduleService;

    @GetMapping("/campus-search-list")
    @ApiOperation(value = "校区搜索列表", response = CampusSearchVO.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "lng", value = "经度", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "lat", value = "纬度", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "productId", value = "商品Id", dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "campusName", value = "校区名称", dataType = "string", paramType = "query")
    })
    public ResponseEntity<List<CampusSearchVO>> campusSearchList(@Valid CampusSearchDTO dto)
        throws JsonProcessingException {

        List<CampusSearchVO> list = organizationCampusService.getCampusSearchList(dto);
        return ResultUtil.success(list);
    }

    /**
     * 社区课程列表
     *
     * @param dto dto
     * @return {@link ResponseEntity}<{@link List}<{@link ProductCourseVO}>>
     */
    @GetMapping("/product-course-list")
    @ApiOperation(value = "社区课程列表", response = ProductCourseVO.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "campusId", value = "校区Id", required = true, dataType = "string", paramType = "query")
    })
    public ResponseEntity<List<ProductCourseVO>> productCourseList(@Valid CampusIdDTO dto) {

        List<ProductCourseVO> list = productService.getProductCourseList(dto);
        return ResultUtil.success(list);
    }

    @GetMapping("/course-details")
    @ApiOperation(value = "商品详情", response = ProductCourseDetailsVO.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "campusId", value = "校区Id", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "productId", value = "商品Id", required = true, dataType = "string", paramType = "query")
    })
    public ResponseEntity<ProductCourseDetailsVO> courseDetails(@Valid ProductCourseDetailsDTO dto) {

        ProductCourseDetailsVO pcd = productService.getCourseDetails(dto);
        return ResultUtil.success(pcd);
    }

    @GetMapping("/near-spell-class-list")
    @ApiOperation(value = "附近拼班列表", response = NearSpellClassVO.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "campusId", value = "校区Id", required = true, dataType = "string", paramType = "query")
    })
    public ResponseEntity<List<NearSpellClassVO>> nearSpellClassList(@Valid CampusIdDTO dto) {

        List<NearSpellClassVO> list = classInfoService.nearSpellClassList(dto);
        return ResultUtil.success(list);
    }

    @GetMapping("/my-spell-class-list")
    @ApiOperation(value = "我要拼班", response = MySpellClassVO.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "campusId", value = "校区Id", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "productId", value = "商品Id", required = true, dataType = "string", paramType = "query")
    })
    public ResponseEntity<MySpellClassVO> mySpellClassList(@Valid ProductCourseDetailsDTO dto) {

        MySpellClassVO msc = productService.getMySpellClassList(dto);
        return ResultUtil.success(msc);
    }

    @PostMapping("/initiate-spell-class")
    @ApiOperation(value = "发起拼班")
    public ResponseEntity initiateSpellClassList(@Valid @RequestBody SpellingClassDetailsDTO dto) {

        String classInfoId = classInfoService.initiateSpellClassList(dto);
        Map<String,Object> result = new HashMap<>();
        result.put("classInfoId",classInfoId);
        return ResultUtil.success(result);
    }

    @GetMapping("/mine-spell-class")
    @ApiOperation(value = "我的拼班", response = MineSpellClassVO.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "classInfoId", value = "班级Id", required = true, dataType = "string", paramType = "query")
    })
    public ResponseEntity<MineSpellClassVO> mineSpellClass(@NotBlank(message = "班级Id不可为空") String classInfoId ) {

        MineSpellClassVO msc = classInfoService.getMineSpellClass(classInfoId);
        return ResultUtil.success(msc);
    }

     @PostMapping("/remove-class-student")
     @ApiOperation(value = "移出学员(退出拼班)")
     public ResponseEntity<String> removeClassStudent(@Valid @RequestBody RemoveStudentDTO dto) throws ParseException {

         classInfoService.removeClassStudent(dto);
         return ResultUtil.success("移出成功");
     }


    @GetMapping("banner")
    @ApiOperation("小程序端首页获取轮播图")
    public ResponseEntity<List<BannerConfigVO>> getBanner(){
        return ResultUtil.success(bannerService.getBanner());
    }

    @PostMapping("disband-class")
    @ApiOperation("解散拼班(团长)")
    public ResponseEntity disbandClass(@RequestBody DisbandClassDTO disbandClassDTO) {
        classInfoService.disbandClass(disbandClassDTO);
        return ResultUtil.success("解散成功");
    }

    @PostMapping("/add-stu")
    @ApiOperation("拼班添加学员")
    public ResponseEntity<Boolean> addStu(@Valid @RequestBody StuSpellClassDTO dto){
        return ResultUtil.success(this.spellClassService.addStu(dto));
    }

    @GetMapping("/course-date")
    @ApiOperation("获取课程日期和节次")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "scheduleId", value = "课表Id", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "startTime", value = "开始时段", required = true, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "endTime", value = "结束时段", required = true, dataType = "string", paramType = "query")
    })
    public ResponseEntity<CourseDateNumVO> getCourseDate(String scheduleId,String startTime,String endTime){
        CourseDateNumVO vo = courseScheduleService.getCourseDate(scheduleId,startTime,endTime);
        return ResultUtil.success(vo);
    }

}
